﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineStore.Dal;

namespace OnlineStore.Bll
{
    public class bizVoucher
    {
        DalVoucher _dalVoucher = new DalVoucher();
        Voucher _VoucherItem = new Voucher();
        public bizVoucher(string ID)
        {
            _VoucherItem = _dalVoucher.LoadVoucher(Convert.ToInt32(ID));
        }
        public bizVoucher()
        {
            // TODO: Complete member initialization
        }
        public bool UpdateVoucher(string Price,DateTime Begin_Date,DateTime End_Date,string Status)
        {
            if (Convert.ToInt32(Price) < 1) return false;
            _VoucherItem.Voucher_Price = Convert.ToInt32(Price);
            _VoucherItem.End_Date = End_Date;
            _VoucherItem.Begin_Date = End_Date;
            _VoucherItem.Status = Convert.ToBoolean(Status);
            return _dalVoucher.Update(_VoucherItem);
        }
        public void getVoucher(string Key)
        {
            if (Key != null && Key != "") {
                _VoucherItem = _dalVoucher.getVoucher(Key);
                if (_VoucherItem.Status == false || DateTime.Now > _VoucherItem.End_Date || DateTime.Now <_VoucherItem.Begin_Date)
                    _VoucherItem = new Voucher();
            }
        }
        public Voucher VoucherItem
        {
            get { return _VoucherItem; }
        }
        public double Values
        {
            get {
                return Convert.ToDouble(_VoucherItem.Voucher_Price);
            }
        }
        public bool SetUsingVoucher()
        {
            
            if (_dalVoucher.SetUsingVoucher(_VoucherItem))
            {
                _VoucherItem = new Voucher();
                return true;
            }
            return false;
        }
        public bool isVoucher(string Key)
        {
            if (Key == null) return true;
            if (Key == "") return false;
            if (_dalVoucher.getVoucher(Key) != null) return true;
            return false;

        }
        public string RandomKey
        {
            get {
                string key; int k = 0;
                do
                {
                    if (k == 9999)
                    {
                        key = null;
                        break;
                    }
                    k++;
                    key = "";
                    Random ran = new Random();
                    for (int i = 0; i < 10; i++)
                        key += ran.Next(10);
                } while (isVoucher(key));
                return key;
            }
        }
        public bool Create(string Key,string Price,DateTime Begin,DateTime End,string Status)
        {
            if (Key.IndexOf(" ") != -1) return false;
            if (Convert.ToDouble(Price) < 1) return false;
            Voucher item = new Voucher();
            item.Voucher_ID = _dalVoucher.MaxID + 1;
            item.Voucher_Key = Key;
            item.Voucher_Price = Convert.ToInt32(Price);
            item.Begin_Date = Begin;
            item.End_Date = End;
            item.Status = Convert.ToBoolean(Status);
            return _dalVoucher.AddToVoucher(item);
        }
    }
}